new Vue({
  el: '#app',
  data() {
    return {
      Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      form: {
        App_Name: "",
        Store_SerialNumber: "",
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      formData: {},
      tableData: [],		//表格数据
      total: 0,
      dialogForm: false,
      title: '',
      store: [],
      batchDelete_list: [],
      formData: {
        Primary_Key_Name: "Enterprise_Appid_SerialNumber",
        Primary_Key: "",
        Data: "enterprise_appid",
      },
      row2: {}
    }
  },
  mounted() {
    this.getData();
    this.getList(this.form);
  },
  complete: {

  },
  methods: {
    onSerach() {				//查询方法
      this.form.page = 1;
      this.getList(this.form)
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form)
    },
    getData() {
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
        type: 'POST',
        data: {
          Data: 'store_info'
        },
        success: function (res) {
          _this.store = res
          console.log('小程序',res)
        },
        error: function () {
          console.log(error)
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getList(data) {
      let _this = this
      $.ajax({
        url: DomainName + '/getEnterpriseAppIdAction?token=' + $.cookie('token'),
        type: 'POST',
        data,
        success: function (res) {
          console.log(res);
          if (res.Status == 'success') {
            _this.tableData = res.tableData
            _this.total = parseInt(res.limit);
            console.log(res.tableData);
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    addEnterprise() {
      if (this.Enterprise_Info_SerialNumber === '1') {
        window.location.href = './Add.html';
      } else{
        alert('无法添加,请联系管理员');
      }
    },
    config(Enterprise_Appid_SerialNumber) {
      sessionStorage.setItem("Enterprise_Appid_SerialNumber", Enterprise_Appid_SerialNumber)
      window.location.href = './Config.html?Enterprise_Appid_SerialNumber=' + Enterprise_Appid_SerialNumber
    },
    editMember(App_ID) {
      sessionStorage.setItem("App_ID", App_ID)
      window.location.href = './Edit.html?App_ID=' + App_ID
      console.log('App_Id',App_ID);
    },
    deletMember(row) {
      const _this = this
      this.$confirm('此操作将删除小程序, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        _this.formData.Primary_Key = row.Enterprise_Appid_SerialNumber;
        _this.get_original_value(_this.formData, (obj) => {_this.Delete(obj);});
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除'
        });
      });
    },
    //查原值
    get_original_value(data, callback) {
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
        type: 'POST',
        data,
        success: function (res) {
          callback(res[0])
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    //删除
    Delete(row) {
      const _this = this;
      let rowData = [];
      // 判断 row 的类型
      if (Array.isArray(row)) {
        // 如果是数组，则取第一个元素作为 rowData
        rowData = [...row];
      } else {
        // 否则，直接使用 row 作为 rowData
        rowData.push(
          row
        );
      }
      rowData.forEach((item) => {
        item.Row_Status = '1'
      })
      let data = {
        Data: JSON.stringify({
          Modify: { enterprise_appid: rowData }
        })
      };
      $.ajax({
        url: DomainName + '/getEditAction?token=' + $.cookie('token'),
        type: 'POST',
        data,
        error: function (array) {
          console.log(array);
          _this.$message.error("删除失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (res) {
          if (res.Status == 'success') {
            _this.$message({
              type: 'success',
              message: '删除成功!'
            });
            _this.getList(_this.form)
          }
        }
      })
    },
    batchDelete() {
      this.$confirm('此操作将选中删除小程序, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.batchDelete_list.forEach((item) => {
          // this.Delete(this.batchDelete_list);
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val) {//多选
      this.batchDelete_list = val;
    },
  },
})